package com.songtech.uitest.module.cancel;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.KakongHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.module.propose.UndertakeTower;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class TowerCancelConfirm {

    @Autowired
    @Qualifier("indentResource")
    private IndentResource indentResource;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(TowerCancelConfirm.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    /**
     * 模拟铁塔进行需求取消确认
     * @param step 铁塔需求取消确认模拟时，十位代表同意与否，0同意，1不同意
     * 个位代表审批者，0为主账号，正整数为其他审核者（会导致新账号重新登录）
     * @throws Exception
     */
    public void towerCancelConfirm(String step) throws Exception{

        boolean approval = stepParser.parseApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "接口模拟", "需求取消确认模拟", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        logger.info(space.getSpace() + "开始填写需求取消确认表单");

        //填表单
        driver.findElement(By.id("towerOrderCode")).sendKeys(indent.getTowerId());

        driver.findElement(By.id("province")).clear();
        driver.findElement(By.id("province")).sendKeys(indentResource.getCancelConfirmProvince());
        driver.findElement(By.id("region")).clear();
        driver.findElement(By.id("region")).sendKeys(indentResource.getCancelConfirmRegion());
        driver.findElement(By.id("county")).clear();
        driver.findElement(By.id("county")).sendKeys(indentResource.getCancelConfirmCountry());

        //是否同意
        Select syncResult = new Select(driver.findElement(By.id("syncResult")));
        syncResult.selectByVisibleText(approval ? "是" : "否");

        //确认人
        driver.findElement(By.id("creator")).sendKeys("xh");
        //确认时间
        driver.findElement(By.id("datetimepicker1")).findElement(By.cssSelector("span.glyphicon.glyphicon-calendar")).click();
        //请求时间
        driver.findElement(By.id("datetimepicker2")).findElement(By.cssSelector("span.glyphicon.glyphicon-calendar")).click();
        //不同意原因
        driver.findElement(By.id("failMessage")).sendKeys(approval ? "" : "铁塔不同意进行需求取消确认的原因巴拉巴拉");

        driver.findElement(By.id("saveSet")).click();
        Thread.sleep(5000);

        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        String opt = approval ? "同意" : "不同意";
        logger.info(space.getSpace() + "模拟铁塔进行需求取消确认表单提交完成，提交意见为" + opt);

        tabHandler.closeAllTabs(driver);

    }


    public void towerCancel3Confirm(String step) throws Exception{

        boolean approval = stepParser.parseApproval(step);
        WebDriver driver = stepParser.parseDriver(step);

        tabHandler.closeAllTabs(driver);
        //打开三方门户需求取消确认列表页
        menuHandler.clickMenuItem(driver, "铁塔建设管理", "需求取消确认", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        logger.info(space.getSpace() + "开始进行三方门户需求取消确认");

        Select taskStatus = new Select(driver.findElement(By.id("taskStatus")));
        taskStatus.selectByVisibleText("待办");
        driver.findElement(By.id("keywords")).sendKeys((indent.getMobileId()));

        driver.findElement(By.xpath("//button[text()='查询']")).click();

        Thread.sleep(6000);
        //搜索并进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='提交']")).click();

        //打开并转到三方门户需求取消确认详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到三方门户需求取消确认详细页
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);

        driver.switchTo().frame(frame);
        //是否同意
        Select syncResult = new Select(driver.findElement(By.id("syncResult")));
        syncResult.selectByVisibleText(approval ? "同意取消" : "不同意取消");

        if(approval == false){
            driver.findElement(By.id("failMessage")).sendKeys("三方门户表示不同意，不解释。");
        }

        driver.findElement(By.id("auditNote")).sendKeys(approval ? "同意此取消操作" : "不同意此取消操作");


        driver.findElement(By.id("saveSet")).click();

        Thread.sleep(5000);

        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        String opt = approval ? "同意" : "不同意";
        logger.info(space.getSpace() + "三方门户进行需求取消确认提交完成，提交意见为" + opt);

        tabHandler.closeAllTabs(driver);

    }

}
